package org.litu.app.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.litu.app.entity.system.SysLogs;
import org.litu.app.service.ISysLogsService;
import org.litu.app.service.ISysSystemService;
import org.litu.base.controller.BaseFormController;
import org.litu.base.log.LtLog;
import org.litu.core.base.ApiRes;
import org.litu.core.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统日志管理controller
 *
 * @author yueye
 */
@RestController
@LtLog(module = "系统日志", enable = false)
@RequestMapping("/system/sysLogs")
@Api(value = "系统日志", tags = {"系统日志表"}, protocols = "http,https")
public class SysLogsController extends BaseFormController<SysLogs, ISysLogsService> {

    @Autowired
    ISysSystemService sysSystemService;

    /**
     * 根据时间删除日志
     *
     * @param startTime 开始时间，日期+时间
     * @param endTime   结束时间，日期+时间
     * @return 成功则返回日志已清空
     */
    @PostMapping("/deleteByTime")
    @ApiOperation(value = "清空指定时间日志", notes = "根据选择的时间段，删除时间段内的所有日志内容。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间，包含时分秒", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间，包含时分秒", required = true),
    })
    public ApiRes deleteByTime(@RequestParam(required = true) String startTime, @RequestParam(required = true) String endTime) {
        if (StringUtils.isAnyBlank(startTime, endTime)) {
            return ApiRes.error(ResultEnum.ParamError);
        }

        boolean res = service.deleteByTime(startTime, endTime);
        return res ? ApiRes.ok(ResultEnum.Success) : ApiRes.error(ResultEnum.UpdateError, "日志删除失败！");
    }
}
